<template>
  <div class="dashboard-container">
    <!-- 第一行 -->
    <el-row :gutter="40" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="user" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              用户总数
            </div>
            <span class="card-panel-num">{{ dashBoardData.user }}</span>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="peoples" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              讲师总数
            </div>
            <span class="card-panel-num">{{ dashBoardData.teacher }}</span>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon icon-class="education" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              课程总数
            </div>
            <span class="card-panel-num">{{ dashBoardData.course }}</span>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon icon-class="shopping" class-name="card-panel-icon"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              订单总数
            </div>
            <span class="card-panel-num">{{ dashBoardData.order }}</span>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
    <div class="chart-container">
      <div id="chart" style="height:500px;width:100%"></div>
    </div>

  </div>
</template>

<script>
  import echarts from 'echarts'
  import  'echarts/theme/macarons.js'
  import { getDashBoard, getChart } from '@/api/dashboard'

  export default {
    data() {
      return {
        dashBoardData: {},
        chart: null,
        title: '',
        xData: [],
        userData: [],
        teacherData: [],
        orderData: [],
        courseData: []
      }
    },
    methods: {
      getDashBoardData() {
        getDashBoard().then(result => {
          let { code, data } = result
          if (code === 20000) {
            this.dashBoardData = data.item
          }
        })
      },

      showChart() {
        this.initChartData()
      },

      // 准备图表数据
      initChartData() {
        getChart().then(result => {
          let { code,data } = result
          if (code === 20000){
            // x轴时间
            this.xData = data.item.dateList
            this.userData = data.item.userList
            this.teacherData = data.item.teacherList
            this.orderData = data.item.orderList
            this.courseData = data.item.courseList
            this.setChart()
          }
        })
      },

      // 设置图标参数
      setChart() {
        // 基于准备好的dom，初始化echarts实例
        this.chart = echarts.init(document.getElementById('chart'),'macarons')

        // 指定图表的配置项和数据
        let option = {
          // x轴是类目轴（离散数据）,必须通过data设置类目数据
          xAxis: {
            type: 'category',
            data: this.xData
          },
          // y轴是数据轴（连续数据）
          yAxis: {
            type: 'value'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          legend: {
            data:['用户注册数','讲师注册数','课程新增数','订单新增数']
          },
          // 系列列表。每个系列通过 type 决定自己的图表类型
          series: [
            {
            name: '用户注册数',
            // 系列中的数据内容数组
            data: this.userData,
            // 折线图
            type: 'line',
            itemStyle : { normal: {label : {show: true}}}
            },
            {
              name: '讲师注册数',
              // 系列中的数据内容数组
              data: this.teacherData,
              // 折线图
              type: 'line',
              itemStyle : { normal: {label : {show: true}}}
            },
            {
              name: '课程新增数',
              // 系列中的数据内容数组
              data: this.courseData,
              // 折线图
              type: 'line',
              itemStyle : { normal: {label : {show: true}}}
            },
            {
              name: '订单新增数',
              // 系列中的数据内容数组
              data: this.orderData,
              // 折线图
              type: 'line',
              itemStyle : { normal: {label : {show: true}}}
            }
          ],
        }

        this.chart.setOption(option)
      }
    },


    created() {
      this.getDashBoardData()
    },
    mounted() {
      this.showChart()
    }
  }
</script>

<style lang="scss" scoped>
  .chart-container{
    background-color: #ffffff;
    padding-top: 20px;
  }
  .dashboard-container{
    padding: 32px;
    background-color: #ffffff;
    position: relative;
  }
  .panel-group {
    margin-top: 10px;

    .card-panel-col {
      margin-bottom: 20px;
    }

    .card-panel {
      height: 108px;
      font-size: 12px;
      position: relative;
      overflow: hidden;
      color: #666;
      background: #fff;
      box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
      border-color: rgba(0, 0, 0, .05);

      &:hover {
        .card-panel-icon-wrapper {
          color: #fff;
        }

        .icon-people {
          background: #40c9c6;
        }

        .icon-message {
          background: #36a3f7;
        }

        .icon-money {
          background: #f4516c;
        }

        .icon-shopping {
          background: #34bfa3
        }
      }

      .icon-people {
        color: #40c9c6;
      }

      .icon-message {
        color: #36a3f7;
      }

      .icon-money {
        color: #f4516c;
      }

      .icon-shopping {
        color: #34bfa3
      }

      .card-panel-icon-wrapper {
        float: left;
        margin: 14px 0 0 14px;
        padding: 16px;
        transition: all 0.38s ease-out;
        border-radius: 6px;
      }

      .card-panel-icon {
        float: left;
        font-size: 48px;
      }

      .card-panel-description {
        float: right;
        font-weight: bold;
        margin: 26px;
        margin-left: 0px;

        .card-panel-text {
          line-height: 18px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 16px;
          margin-bottom: 12px;
        }

        .card-panel-num {
          font-size: 20px;
        }
      }
    }
  }

  @media (max-width:550px) {
    .card-panel-description {
      display: none;
    }

    .card-panel-icon-wrapper {
      float: none !important;
      width: 100%;
      height: 100%;
      margin: 0 !important;

      .svg-icon {
        display: block;
        margin: 14px auto !important;
        float: none !important;
      }
    }
  }
</style>
